package com.apes.scm.masterdata.merchant.model;

import com.alibaba.fastjson.JSONArray;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.dcDeliveryArea.model.DcDeliveryArea;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.Organization;
import com.apes.scm.masterdata.frame.model.Store;
import com.apes.scm.masterdata.merchant.service.MerchantService;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能：商户
 *
 * @author zhangwencong
 * @create 2022-04-28 14:54
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Merchant.CONST_TABLE_NAME)

public class Merchant extends  Model {

    public static final String CONST_TABLE_NAME = "mer_merchant";

    @Id
    @Column(name = "id")
    @Comment("商户ID")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商户ID")
    private String id; //商户编码

    @Name
    @Column(name = "name", nullable = false, length = 80)
    @Comment("商户")
    private String name; //商户名称

    @Column(name = "valid",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    @Comment("有效标记")
    private boolean valid = true;

    public JSONArray getStoreInfo() {
        if (this.id ==null) return new JSONArray();
        JSONArray storeInfo = SpringManager.getBean(MerchantService.class).getStoreInfo(this);
        this.storeInfo = storeInfo;
        return this.storeInfo;
    }

    @Transient
    private JSONArray storeInfo;
}
